package com.utils.tools;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class TreeDataUtil {


    /**
     * 将父子关系数据生产父子结构数据，pid是代表节点id
     *
     * @param menuList
     * @param pid
     * @return
     */
    public static List<Map> buildTreeData(List<Map> menuList, String pid) {
        List<Map> treeList = new ArrayList<>();
        menuList.forEach(obj -> {
            if (obj.get("parentId").equals(pid)) {
                List<Map> listChildren = buildTreeData(menuList, String.valueOf(obj.get("id")));
                if (listChildren != null && listChildren.size() > 0) {
                    obj.put("children", listChildren);
                }
                treeList.add(obj);
            }
        });
        if (treeList.isEmpty()) {
            return null;
        }
        return treeList;
    }
}
